home *** CD-ROM | disk | FTP | other *** search
/ Cre@te Online 2000 December / Cre@teOnline CD05.iso / MacSoft / XML ConsoleMax.sea / XML ConsoleMax / Required / swingall.jar / javax / swing / undo / UndoManager.class (.txt) < prev   
Encoding:
Java Class File  |  1999-07-15  |  4.2 KB  |  233 lines

  1. package javax.swing.undo;
  2.  
  3. import java.util.Enumeration;
  4. import java.util.Vector;
  5. import javax.swing.event.UndoableEditEvent;
  6. import javax.swing.event.UndoableEditListener;
  7.  
  8. public class UndoManager extends CompoundEdit implements UndoableEditListener {
  9.    int indexOfNextAdd = 0;
  10.    int limit = 100;
  11.  
  12.    public UndoManager() {
  13.       super.edits.ensureCapacity(this.limit);
  14.    }
  15.  
  16.    public synchronized boolean addEdit(UndoableEdit var1) {
  17.       this.trimEdits(this.indexOfNextAdd, super.edits.size() - 1);
  18.       boolean var2 = super.addEdit(var1);
  19.       if (super.inProgress) {
  20.          var2 = true;
  21.       }
  22.  
  23.       this.indexOfNextAdd = super.edits.size();
  24.       this.trimForLimit();
  25.       return var2;
  26.    }
  27.  
  28.    public synchronized boolean canRedo() {
  29.       if (!super.inProgress) {
  30.          return super.canRedo();
  31.       } else {
  32.          UndoableEdit var1 = this.editToBeRedone();
  33.          return var1 != null && var1.canRedo();
  34.       }
  35.    }
  36.  
  37.    public synchronized boolean canUndo() {
  38.       if (!super.inProgress) {
  39.          return super.canUndo();
  40.       } else {
  41.          UndoableEdit var1 = this.editToBeUndone();
  42.          return var1 != null && var1.canUndo();
  43.       }
  44.    }
  45.  
  46.    public synchronized boolean canUndoOrRedo() {
  47.       return this.indexOfNextAdd == super.edits.size() ? this.canUndo() : this.canRedo();
  48.    }
  49.  
  50.    public synchronized void discardAllEdits() {
  51.       Enumeration var1 = super.edits.elements();
  52.  
  53.       while(var1.hasMoreElements()) {
  54.          UndoableEdit var2 = (UndoableEdit)var1.nextElement();
  55.          var2.die();
  56.       }
  57.  
  58.       super.edits = new Vector(this.limit);
  59.       this.indexOfNextAdd = 0;
  60.    }
  61.  
  62.    protected UndoableEdit editToBeRedone() {
  63.       int var1 = super.edits.size();
  64.       int var2 = this.indexOfNextAdd;
  65.  
  66.       while(var2 < var1) {
  67.          UndoableEdit var3 = (UndoableEdit)super.edits.elementAt(var2++);
  68.          if (var3.isSignificant()) {
  69.             return var3;
  70.          }
  71.       }
  72.  
  73.       return null;
  74.    }
  75.  
  76.    protected UndoableEdit editToBeUndone() {
  77.       int var1 = this.indexOfNextAdd;
  78.  
  79.       while(var1 > 0) {
  80.          --var1;
  81.          UndoableEdit var2 = (UndoableEdit)super.edits.elementAt(var1);
  82.          if (var2.isSignificant()) {
  83.             return var2;
  84.          }
  85.       }
  86.  
  87.       return null;
  88.    }
  89.  
  90.    public synchronized void end() {
  91.       super.end();
  92.       this.trimEdits(this.indexOfNextAdd, super.edits.size() - 1);
  93.    }
  94.  
  95.    public synchronized int getLimit() {
  96.       return this.limit;
  97.    }
  98.  
  99.    public synchronized String getRedoPresentationName() {
  100.       if (super.inProgress) {
  101.          return this.canRedo() ? this.editToBeRedone().getRedoPresentationName() : "Redo";
  102.       } else {
  103.          return super.getRedoPresentationName();
  104.       }
  105.    }
  106.  
  107.    public synchronized String getUndoOrRedoPresentationName() {
  108.       return this.indexOfNextAdd == super.edits.size() ? this.getUndoPresentationName() : this.getRedoPresentationName();
  109.    }
  110.  
  111.    public synchronized String getUndoPresentationName() {
  112.       if (super.inProgress) {
  113.          return this.canUndo() ? this.editToBeUndone().getUndoPresentationName() : "Undo";
  114.       } else {
  115.          return super.getUndoPresentationName();
  116.       }
  117.    }
  118.  
  119.    public synchronized void redo() throws CannotRedoException {
  120.       if (super.inProgress) {
  121.          UndoableEdit var1 = this.editToBeRedone();
  122.          if (var1 == null) {
  123.             throw new CannotRedoException();
  124.          }
  125.  
  126.          this.redoTo(var1);
  127.       } else {
  128.          super.redo();
  129.       }
  130.  
  131.    }
  132.  
  133.    protected void redoTo(UndoableEdit var1) throws CannotRedoException {
  134.       UndoableEdit var3;
  135.       for(boolean var2 = false; !var2; var2 = var3 == var1) {
  136.          var3 = (UndoableEdit)super.edits.elementAt(this.indexOfNextAdd++);
  137.          var3.redo();
  138.       }
  139.  
  140.    }
  141.  
  142.    public synchronized void setLimit(int var1) {
  143.       this.limit = var1;
  144.       this.trimForLimit();
  145.    }
  146.  
  147.    public String toString() {
  148.       return super.toString() + " limit: " + this.limit + " indexOfNextAdd: " + this.indexOfNextAdd;
  149.    }
  150.  
  151.    protected void trimEdits(int var1, int var2) {
  152.       if (var1 <= var2) {
  153.          for(int var3 = var2; var1 <= var3; --var3) {
  154.             UndoableEdit var4 = (UndoableEdit)super.edits.elementAt(var3);
  155.             var4.die();
  156.             super.edits.removeElementAt(var3);
  157.          }
  158.  
  159.          if (this.indexOfNextAdd > var2) {
  160.             this.indexOfNextAdd -= var2 - var1 + 1;
  161.          } else if (this.indexOfNextAdd >= var1) {
  162.             this.indexOfNextAdd = var1;
  163.          }
  164.       }
  165.  
  166.    }
  167.  
  168.    protected void trimForLimit() {
  169.       if (this.limit > 0) {
  170.          int var1 = super.edits.size();
  171.          if (var1 > this.limit) {
  172.             int var2 = this.limit / 2;
  173.             int var3 = this.indexOfNextAdd - 1 - var2;
  174.             int var4 = this.indexOfNextAdd - 1 + var2;
  175.             if (var4 - var3 + 1 > this.limit) {
  176.                ++var3;
  177.             }
  178.  
  179.             if (var3 < 0) {
  180.                var4 -= var3;
  181.                var3 = 0;
  182.             }
  183.  
  184.             if (var4 >= var1) {
  185.                int var5 = var1 - var4 - 1;
  186.                var4 += var5;
  187.                var3 += var5;
  188.             }
  189.  
  190.             this.trimEdits(var4 + 1, var1 - 1);
  191.             this.trimEdits(0, var3 - 1);
  192.          }
  193.       }
  194.  
  195.    }
  196.  
  197.    public synchronized void undo() throws CannotUndoException {
  198.       if (super.inProgress) {
  199.          UndoableEdit var1 = this.editToBeUndone();
  200.          if (var1 == null) {
  201.             throw new CannotUndoException();
  202.          }
  203.  
  204.          this.undoTo(var1);
  205.       } else {
  206.          super.undo();
  207.       }
  208.  
  209.    }
  210.  
  211.    public synchronized void undoOrRedo() throws CannotRedoException, CannotUndoException {
  212.       if (this.indexOfNextAdd == super.edits.size()) {
  213.          this.undo();
  214.       } else {
  215.          this.redo();
  216.       }
  217.  
  218.    }
  219.  
  220.    protected void undoTo(UndoableEdit var1) throws CannotUndoException {
  221.       UndoableEdit var3;
  222.       for(boolean var2 = false; !var2; var2 = var3 == var1) {
  223.          var3 = (UndoableEdit)super.edits.elementAt(--this.indexOfNextAdd);
  224.          var3.undo();
  225.       }
  226.  
  227.    }
  228.  
  229.    public void undoableEditHappened(UndoableEditEvent var1) {
  230.       this.addEdit(var1.getEdit());
  231.    }
  232. }
  233.